global.lzss_encode = function(src=[]){
    var newlist = [];
    var offset = -1;
    var _16bit = true;

    var list = new this.Uint8Array(src.length * (_16bit?2:1));
    src.forEach((v,k)=>{
        if(!_16bit){
            list[k] = v;
        }
        else{
            list[k*2+0] = v&0xff;
            list[k*2+1] = (v>>8)&0xff;
        }
    });



    var head_offset = 0;
    var bit_offset = -1;

    while(++offset < list.length){
        //搜索已经存在的数据
        var start = offset - 1;
        var maxLen = {offset:0,len:0};

        while(1){
            if(start<0||start>0xffe){
                //结束搜索
                break;
            }
            //判断是否相等
            var idx = 0;
            while(list[start+idx]==list[offset+idx]){
                idx++;
            }
            //console.log(start+idx);

            // if(idx>5){
            //     console.log(idx);
            // }


            if(idx>=0xf+3){
                //达到最长数据复制，可以退出
                idx = 0xf+3;
                maxLen.len = idx;
                maxLen.offset = start;
                break;
            }
            //最长数据比较
            if(idx>maxLen.len){
                maxLen.len = idx;
                maxLen.offset = start;
            }
            start--;
        }

        bit_offset -= 1;
        if(bit_offset<0){
            //头字节已经满出来了
            // if(newlist[head_offset]!=undefined)
            //     console.log(newlist.length.toString(16),newlist[head_offset].toString(16));
            newlist.push(0);
            head_offset = newlist.length - 1;
            bit_offset = 7;
        }

        if(maxLen.len>2){
            newlist[head_offset] |= (1<<bit_offset);
            //如果大于3，则可以压缩
            var winfo = (offset) - (maxLen.offset+1);   //这里的1是往前移一个,12bit
            winfo |= ((maxLen.len-3)<<12);          //复制的大小，站4bit
            //if(head_offset<0x20)
                //console.log("len:",maxLen.len,offset,maxLen.offset);
            //需要交换高低位
            var tmp = winfo & 0xff;
            var tmp2 = winfo >> 8;
            winfo = tmp2 | (tmp<<8);            //窗口信息

            newlist.push(tmp2);
            newlist.push(tmp);
            offset += maxLen.len-1;
        }
        else{
            //当前数据压缩
            var v = list[offset];
            newlist.push(list[offset]);
        }

    }

    //console.log(newlist.length/2,list.length)

    var buf = new this.Uint8Array(newlist.length+4);
    buf[0] = 0x10;
    buf[1] = list.length & 0xff;
    buf[2] = (list.length>>8) & 0xff;
    buf[3] = (list.length>>16) & 0xff;
    newlist.forEach((v,k)=>{
        buf[k+4] = v;
    });


    return buf;
}
